<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>评论</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 顶部搜索框区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getCommentList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getCommentList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 评论表格区域 -->
      <el-table
        :data="commentList"
        stripe
        border
        @selection-change="handleSelectionChange1"
        v-if="value == 1"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="userName" label="评论用户" width="200">
        </el-table-column>
        <el-table-column prop="userName" label="回复用户" width="200">
        </el-table-column>
        <el-table-column
          prop="commentContent"
          label="评论内容"
        ></el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <!-- <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="gotoEditTests(scope.row)"
              >编辑</el-button
            > -->
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="mini"
              @click="deleteTest(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      //   考试列表
      commentList: [],
      //小测列表
      quizsList: [],
      //   选择考试还是小测
      options: [
        {
          value: "1",
          label: "考试"
        },
        {
          value: "2",
          label: "小测"
        }
      ],
      value: "1",
      // 删除评论
      commentDeleteIdList: []
    };
  },
  methods: {
    // 获取评论列表
    async getCommentList() {
      //   const { data: res } = await this.$http.get("goods", {
      //     params: this.queryInfo
      //   });
      //   if (res.meta.status !== 200)
      //     return this.$message.error("获取商品列表失败！");
      //   this.total = res.data.total;
      //   this.commentList = res.data.goods;
      let res = [
        [
          {
            articleCommentId: 1,
            userId: 1,
            articleId: 1,
            commentContent: "第一条评论",
            commentClass: 1,
            toCommentId: -1,
            userVo: null
          },
          {
            articleCommentId: 4,
            userId: 3,
            articleId: 1,
            commentContent: "回复第一条",
            commentClass: 1,
            toCommentId: 1,
            userVo: {
              userId: 1,
              userName: "测试失败乐观锁",
              userIdentity: 2,
              isAdmin: 0
            }
          },
          {
            articleCommentId: 5,
            userId: 4,
            articleId: 1,
            commentContent: "回复回复第一条评论的评论",
            commentClass: 1,
            toCommentId: 4,

            userVo: {
              userId: 3,
              userName: "补录",
              userIdentity: 3,
              isAdmin: 0
            }
          },
          {
            articleCommentId: 9,
            userId: 33,
            articleId: 1,
            commentContent: "用户33回复第一条评论",
            commentClass: 1,
            toCommentId: 1,
            userVo: {
              userId: 1,
              userName: "测试失败乐观锁",
              userIdentity: 2,
              isAdmin: 0
            }
          }
        ],
        [
          {
            articleCommentId: 2,
            userId: 2,
            articleId: 1,
            commentContent: "另一条评论",
            commentClass: 2,
            toCommentId: -1,
            userVo: null
          },
          {
            articleCommentId: 3,
            userId: 2,
            articleId: 1,
            commentContent: "另一条评论",
            commentClass: 2,
            toCommentId: -1,
            userVo: null
          },
          {
            articleCommentId: 6,
            userId: 5,
            articleId: 1,
            commentContent: "回复第二条评论",
            commentClass: 2,
            toCommentId: 3,
            userVo: {
              userId: 2,
              userName: "补测",
              userIdentity: 3,
              isAdmin: 0
            }
          }
        ],
        [
          {
            articleCommentId: 7,
            userId: 6,
            articleId: 1,
            commentContent: "发表高论",
            commentClass: 3,
            toCommentId: -1,
            userVo: null
          },
          {
            articleCommentId: 8,
            userId: 7,
            articleId: 1,
            commentContent: "总座高见",
            commentClass: 3,
            toCommentId: 7,
            userVo: {
              userId: 6,
              userName: "小张",
              userIdentity: 1,
              isAdmin: 0
            }
          }
        ]
      ];
      res.forEach(item => {
        item.forEach(item1 => {
          let exp = item1;
          if (item1.userVo) {
            exp.userName = item1.userVo.userName;
          } else {
            exp.userName = "文章";
          }
          this.commentList.push(exp);
        });
      });
      console.log(this.commentList);
    },
    // 多选勾选时将对应Id添加到删除序列
    handleSelectionChange1(rows) {
      this.commentDeleteIdList = [];
      rows.forEach(item => {
        this.commentDeleteIdList.push(item.articleCommentId);
      });
      console.log(this.commentDeleteIdList);
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCommentList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCommentList();
    },
    // 删除单个评论
    async deleteTest(id) {
      const deleteResult = await this.$confirm("是否删除该评论?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err => err);
      if (deleteResult === "cancel") return this.$message.info("取消删除！");
      //   const { data: res } = await this.$http.delete("goods/" + id);
      //   if (res.meta.status !== 200) return this.$message.error("删除商品失败！");
      this.$message.success("删除成功！");
      this.getCommentList();
    },
    //   删除多个评论
    async deleteTests() {
      const deleteResult = await this.$confirm("是否删除该评论?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err => err);
      if (deleteResult === "cancel") return this.$message.info("取消删除！");
      if (this.value == "1") {
      } else {
      }
      //   const { data: res } = await this.$http.delete("goods/" + id);
      //   if (res.meta.status !== 200) return this.$message.error("删除商品失败！");
      this.$message.success("删除成功！");
      this.getCommentList();
    }
  },
  created() {
    this.getCommentList();
  }
};
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
</style>